package org.langqiao.algorithm;
public class TriangleMatrix {  
     
	public static void main(String[] args) {  
        triangleMatrix(5);  
    }
     
    private static void triangleMatrix(int num) {  
        int matrix[][] = new int[num][num];  
        int row = 0,column = 0;  
        int direction = 0; 
        //方向，取值范围：0，1，2  
        int digit = 1;  
        for(int i = num;i>=1;i--){  
        	for(int j = i;j>=1;j--){  
               if(j==1){ 
            	   //到达某个方向的终点，改变方向  
                   direction = (direction+1)%3;  
               }  
               switch(direction){  
               case 0: 
            	   // 向下  
                   matrix[row++][column] = digit++;  
                   break;  
               case 1: 
            	   // 向右  
                   matrix[row][column++]=digit++;  
                   break;  
               case 2: 
            	   // 斜向上  
                   matrix[row--][column--] = digit++;  
                   break;  
                 }  
             }  
         }  
         for(int i = 0;i<num;i++){  
             for(int j = 0;j<=i;j++){  
                 System.out.print(matrix[i][j]+"\t");  
             }  
             System.out.println();  
        }  
     }  
 }    
